package com.fc.v2.controller;

import cn.hutool.core.util.RandomUtil;
import com.fc.v2.config.multiSource.DynamicDataSource;
import com.fc.v2.domain.AjaxResult;
import com.fc.v2.model.auto.SysUser;
import com.fc.v2.model.auto.SysUserExample;
import com.fc.v2.model.custom.Tablepar;
import com.fc.v2.service.auto.ISysTenantService;
import com.fc.v2.service.auto.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/SysUserController")
public class SysUserController {
    private final SysUserService sysUserService;
    private final ISysTenantService iSysTenantService;

    public SysUserController(SysUserService sysUserService, ISysTenantService iSysTenantService) {
        this.sysUserService = sysUserService;
        this.iSysTenantService = iSysTenantService;
    }


    @GetMapping("/list")
    public PageInfo<SysUser> list(Tablepar tablepar){


        //测试切换数据源
        int i= RandomUtil.randomInt(1,3);
        if(i==1){
            System.out.println("1111111111");
            DynamicDataSource.setDataSource("001");
        }else{
            System.out.println("2222222222");
            DynamicDataSource.setDataSource("002");
        }
        //测试切换数据源
      PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
      SysUserExample sysUserExample=new SysUserExample();
      List<SysUser> list= sysUserService.selectByExample(sysUserExample);
      PageInfo<SysUser> pageInfo = new PageInfo<SysUser>(list);
      return pageInfo;
    }

    @GetMapping("/list2")
    public Object list2(String videoId, HttpServletRequest request){
        System.out.println(videoId);
        System.out.println("-----"+iSysTenantService.getById(1).gettName());
        List<SysUser> list=sysUserService.selectByExample(null);
        System.out.println(request.getServerName()+":"+request.getServerPort());

        return  list;
    }

    /**
     * 根据用户id获取用户
     * @params [userId]
     * @returns com.fc.v2.domain.AjaxResult
     * @Date 2022/1/23 23:02
     * @Created by 87766867@qq.com
     */
    @GetMapping("/getUserId/{userId}")
    public AjaxResult getUserId(@PathVariable("userId") String userId){
        return AjaxResult.successData(sysUserService.selectByPrimaryKey(userId));
    }

    /**
     * 添加注释
     * @params [sysUser]
     * @returns com.fc.v2.domain.AjaxResult
     * @Date 2022/1/23 23:01
     * @Created by 87766867@qq.com
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysUser sysUser){
        return AjaxResult.successData(sysUserService.insertSelective(sysUser));
    }
    /**
     * 修改用户
     * @params [sysUser]
     * @returns com.fc.v2.domain.AjaxResult
     * @Date 2022/1/23 23:03
     * @Created by 87766867@qq.com
     */
    @PutMapping("/update")
    public AjaxResult update(@RequestBody SysUser sysUser){
        return AjaxResult.successData(sysUserService.updateByPrimaryKeySelective(sysUser));
    }
    /**
     * 删除用户
     * @params [userId]
     * @returns com.fc.v2.domain.AjaxResult
     * @Date 2022/1/23 23:03
     * @Created by 87766867@qq.com
     */
    @DeleteMapping("/delete/{userId}")
    public  AjaxResult delete(@PathVariable("userId") String userId){
        return AjaxResult.successData(sysUserService.deleteByPrimaryKey(userId));
    }


}
